<?php
	class ReplyAction extends Action{
		/*加减分*/
		public function checkVote2(){
			$id = (int)$_POST['id'];
			$kind = (int)$_POST['kind'];
			if(in_array($id,explode('%',$_COOKIE['vote2Id']))){
				echo '-1-';
			}else{
				if($kind<0){
					M('News')->where("id = $id")->setInc('no');
					$vote = explode(',',$_COOKIE['vote2Id']);
					$vote[] = $id;
					$vote2Id = implode('%',$vote);
					setcookie('vote2Id',$vote2Id,$_SERVER['REQUEST_TIME']+100000000);
					echo '-1';
				}else{
					M('News')->where("id = $id")->setInc('yes');
					$vote = explode(',',$_COOKIE['vote2Id']);
					$vote[] = $id;
					$vote2Id = implode('%',$vote);
					setcookie('vote2Id',$vote2Id,$_SERVER['REQUEST_TIME']+100000000);
					echo '+1';
				}
			}
		}
		public function showReply(){
			$id = (int)$_POST['id'];
			$reply = M()->Table(array(C('DB_PREFIX').'reply'=>'reply',C('DB_PREFIX').'user'=>'user'))->where("reply.pid = $id AND reply.state = 1 AND reply.arid = user.uid")->order('reply.artime ASC')->select();
			$echo = '<div class="comments" id="reply-'.$id.'">';
			$echo.= '<div>';
			$i = 1;
			foreach($reply as $key=>$value){
				$echo.= '<div id="comment-'.$value['id'].'" class="comment-block">';
				$echo.= '<div class="clearfix">';
				$echo.= '<div class="avatar">';
				$echo.= '<img src="'.C('YICMS_PUBLIC').'/face/f_'.$value['face'].'"/>';
				$echo.= '</div>';
				$echo.= '<div class="reply">';
				$echo.= '<span class="userlogin"><a href="'.U('User/Index/index?uid='.$value['arid']).'">'.$value['uname'].'</a></span>';
				$echo.= '<span class="body">'.$value['arcontent'].'</span>';
				$echo.= '</div>';
				$echo.= '<div class="report"><a href="javascript:showReport('.$value['id'].')" class="link">举报</a> &nbsp;<span title="'.date('Y-m-d H:i:s',$value['artime']).'">'.$i.'</span></div>';
				$echo.= '</div>';
				$echo.= '</div>';
				$i++;
			}
			$echo.= '</div>';
			$echo.= '<div class="input-block">';
			$reply = C('reply');
			if(empty($_SESSION['uid']) && $reply){
				$echo.= '<div class="logout login-block"><a href="javascript:showLoginForm()">登录</a>后才能发表评论</div>';
			}else{
				$echo.= '<form method="post">';
				$echo.= '<textarea class="comment_input" id="arcontent-'.$id.'" name="arcontent">';
				$echo.= '请不要发表与本内容无关的评论，您有了账号就是有身份的人了，我们可认识您。';
				$echo.= '</textarea>';
				$echo.= '<div class="row">';
				$echo.= '<input type="hidden" name="hidden-'.$id.'"/>';
				$echo.= '<button type="button" id="reply_submit" onclick="checkReply2('.$id.')">发表</button>';
				$echo.= '<label for="comment_anonymous"><input id="anonymous-'.$id.'" type="checkbox" value="1" name="anonymous"/> 匿名评论</label>';
				$echo.= '</form>';
				$echo.= '</div>';
			}
			$echo.= '</div>';
			$echo.= '</div>';
			echo $echo;
		}
		public function checkReply2(){
			$reply = C('reply');
			if(empty($_SESSION['uid']) && !$reply){
				$_POST['arid'] = '1';
				$_POST['arname'] = '匿名'; 
			}else{
				$_POST['arid'] = empty($_POST['anonymous']) ? $_SESSION['uid'] : '1';
				$_POST['arname'] = empty($_POST['anonymous']) ? $_SESSION['uname'] : '匿名';
			}
			$_POST['arcontent'] =  htmlspecialchars($_POST['arcontent']);
			$_POST['artime'] = $_SERVER['REQUEST_TIME'];
			$_POST['pid'] = (int)$_POST['pid'];
			$_POST['ip'] = ip2long(get_client_ip());
			$_POST['state'] = '1';
			if(empty($_POST['arcontent']) || $_POST['arcontent'] == '请不要发表与本内容无关的评论，您有了账号就是有身份的人了，我们可认识您。'){
				echo '0';
			}else if(M('Reply')->add($_POST)){
				$uid = $_POST['arid'];
				M('User')->where("uid = $uid")->setInc('comments');
				$id = $_POST['pid'];
				M('News')->where("id = $id")->setInc('reply');
				echo '1';
			}else{
				echo '-1';
			}
		}
		public function checkReply(){
			$reply = C('reply');
			if(empty($_SESSION['uid']) && !$reply){
				$_POST['arid'] = '1';
				$_POST['arname'] = '匿名'; 
			}else{
				$_POST['arid'] = empty($_POST['anonymous']) ? $_SESSION['uid'] : '1';
				$_POST['arname'] = empty($_POST['anonymous']) ? $_SESSION['uname'] : '匿名';
			}
			$_POST['arcontent'] =  htmlspecialchars($_POST['arcontent']);
			$_POST['artime'] = $_SERVER['REQUEST_TIME'];
			$_POST['pid'] = (int)$_POST['pid'];
			$_POST['ip'] = ip2long(get_client_ip());
			$_POST['state'] = '1';
			if(empty($_POST['arcontent']) || $_POST['arcontent'] =='请不要发表与本内容无关的评论，您有了账号就是有身份的人了，我们可认识您。') $this->error('回复就得有内容嘛!');
			if(M('Reply')->add($_POST)){
				$id = $_POST['pid'];
				M('User')->where("uid = $uid")->setInc('comments');
				M('News')->where("id = $id")->setInc('reply');
				$this->success('发表成功!');
			}else{
				$this->error('发表失败!');
			}
		}
	}

?>